//********************************************************************
// Pocket Podcasts - Desktop and mobile device podcast client
// Copyright (C) 2005, 2006 Wes Henderson <whenders@gmail.com> and
// Contributors
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
//********************************************************************

using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
using System.Windows.Forms;

namespace PocketPodcasts
{
	/// <summary>
	/// Subscribe class
	/// </summary>
	/// <remarks>
	/// Authors: Justin Tarnoff, Eric Yee - 2/26/06
	/// Updated 4/3/06, Wes Henderson, Eric Yee - Unsubscribe
	public class Subscriptions
	{
		private ArrayList SubscriptionList;
		private string xmlLoc;

		public Subscriptions()
		{
#if DESKTOP
			xmlLoc = Application.UserAppDataPath + "\\Subscriptions.xml";
#endif
#if MOBILE
			xmlLoc = Globals.AppSettings.DownloadedPodcastDir + "\\Subscriptions.xml";
#endif
		}

		public void LoadSubscriptions()
		{
			SubscriptionList = new ArrayList();
			ReadXml(xmlLoc);
		}

		/// <summary>
		/// Adds a Subscription to the Subscriptions xml file
		/// </summary>
		/// <param name="podcastTitle">The title of the podcast</param>
		/// <param name="podcastID">The podcast's unique database ID</param>
		/// <returns>1 if successfully added, 0 if not added, -1 if parameters are invalid.</returns>
		public int AddSubscription(string podcastTitle, int podcastID)
		{
			if (!IsSubscribed(podcastID))
			{
				SubscriptionInfo temp = new SubscriptionInfo();
				temp.podcastTitle = podcastTitle;
				temp.podcastID = podcastID;
				temp.autoDownload = true;
				SubscriptionList.Add(temp);
				StoreSubscriptions();
				return 1;  // added
			}
			else
				return 0; // did not add
		}

		/// <summary>
		/// Removes a Subscription from the Subscriptions xml file
		/// </summary>
		/// <param name="podcastID">The podcast's unique database ID</param>
		public bool RemoveSubscription(int podcastID)
		{
			foreach (SubscriptionInfo si in SubscriptionList)
			{
				if (si.podcastID == podcastID)
				{
					SubscriptionList.Remove(si);
					StoreSubscriptions();
					return true;
				}
			}
			
			// not found
			return false;
		}

		/// <summary>
		/// Returns true if podcast is already in Subscriptions.xml.
		/// </summary>
		/// <param name="podcastID">The podcast's unique database ID</param>
		public bool IsSubscribed(int podcastID)
		{
			foreach (SubscriptionInfo si in SubscriptionList)
				if (si.podcastID == podcastID)
					return true;

			return false;
		}

		/// <summary>
		/// Stores SubscriptionList into Subscriptions.xml 
		/// Uses <seealso cref="Subscribe.WriteXml"/>
		/// </summary>
		public void StoreSubscriptions()
		{
			WriteXml(SubscriptionList);
		}

		//public SubscriptionInfo this[int index]
		//{
		//    get
		//    {
		//        return (SubscriptionInfo)SubscriptionList[index];
		//    }
		//    set
		//    {
		//        SubscriptionList[index] = (SubscriptionInfo)value;
		//    }
		//}

		public int Count
		{
			get { return SubscriptionList.Count; }
		}

		/// <summary>
		/// An array of SubscribeInfo 
		/// </summary>
		public SubscriptionInfo[] Items
		{
			get
			{
				SubscriptionInfo[] items = new SubscriptionInfo[this.SubscriptionList.Count];
				SubscriptionList.CopyTo(items);
				return items;
			}

			set
			{
				if (value == null) return;
				SubscriptionInfo[] items = (SubscriptionInfo[])value;
				SubscriptionList.Clear();
				foreach (SubscriptionInfo item in items)
					SubscriptionList.Add(item);
			}
		}

		private bool WriteXml(ArrayList SubscriptionList)
		{
			try
			{
				if (SubscriptionList != null)
				{
					XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(SubscriptionInfo) });
					TextWriter tw = new StreamWriter(xmlLoc);
					serializer.Serialize(tw, SubscriptionList);
					tw.Close();
					return true;
				}
				else
					return false;
			}
			catch
			{
				return false;
			}
		}

		private bool ReadXml(string xmlLoc)
		{
			if (File.Exists(xmlLoc))
			{
				XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(SubscriptionInfo) });
				TextReader tr = new StreamReader(xmlLoc);
				try
				{
					SubscriptionList = (ArrayList)serializer.Deserialize(tr);
				}
				catch
				{
					return false;
				}
				tr.Close();
				return true;

			}
			else
				return false;
		}

	}
}